//
// File:	Input.hpp
// Author:	Thomas TISSOT-DUPONT
// Login:	<thomas.tissotdupont@free.fr>
//
// Create:	30/01/2010 at 05:09:39
// Purpose:	
//
#ifndef INPUT_HPP
#define INPUT_HPP

#include <map>
#include "IInput.hpp"

//------------------------------------------------------------------
// Input Class
//------------------------------------------------------------------
template <typename T>
class Input : public IInput
{
public:
									~Input() {}

public:
	const std::type_info			&GetType() const
	{
		return typeid(T);
	}

	void							Bind(T input, const std::string &inputAction)
	{
		Unbind(input);
		m_inputNames[inputAction][input] = false;
	}

	void							Unbind(T input)
	{
		typename InputNamesMap::iterator nameIter = m_inputNames.begin();
		typename InputNamesMap::iterator nameEndIter = m_inputNames.end();
		for (; nameIter != nameEndIter; ++nameIter)
		{
			typename InputMap::iterator inputIter = nameIter->second.begin();
			typename InputMap::iterator inputEndIter = nameIter->second.end();
			for (; inputIter != inputEndIter; ++inputIter)
			{
				if (inputIter->first == input)
				{
					inputIter = nameIter->second.erase(inputIter);
				}
			}
		}
	}

	bool							HasInput(const std::string &inputAction) const
	{
		typename InputNamesMap::const_iterator nameIter = m_inputNames.find(inputAction);
		if (nameIter == m_inputNames.end())
			return false;
		typename InputMap::const_iterator inputIter = nameIter->second.begin();
		typename InputMap::const_iterator inputEndIter = nameIter->second.end();
		for (; inputIter != inputEndIter; ++inputIter)
		{
			if (inputIter->second == true)
			{
				return true;
			}
		}
		return false;
	}

protected:
	typedef std::map<T, bool>					InputMap;
	typedef std::map<std::string, InputMap>		InputNamesMap;
	InputNamesMap								m_inputNames;
};

#endif //!INPUT_HPP
